<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/admin/layuimini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/layuimini/css/public.css" media="all">
    <link rel="stylesheet" href="/static/admin/layuimini/lib/font-awesome-4.7.0/css/font-awesome.min.css">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset" permission="sys:user:query">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input"
                                       placeholder="不输入则忽略用户名筛选">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">邮箱</label>
                            <div class="layui-input-inline">
                                <input type="text" name="email" autocomplete="off" class="layui-input"
                                       placeholder="不输入则忽略邮箱筛选">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">日期范围</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="date-range" placeholder="不输入则忽略日期范围筛选"
                                       name="dateRange">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                            <button type="reset" class="layui-btn layui-btn-primary"><i
                                    class="layui-icon layui-icon-fonts-clear"></i> 清 空
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add" permission="sys:user:add">
                    添加用户
                </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"
                        permission="sys:user:delete">批量删除
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit" permission="sys:user:edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"
               permission="sys:user:delete">删除</a>
        </script>

    </div>
</div>
<script src="/static/admin/layuimini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/static/admin/layuimini/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="/static/admin/my/js/my.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.js"></script>
<script>
    layui.use(['form', 'table', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate;

        //日期范围
        laydate.render({
            elem: '#date-range',
            range: '~',
            theme: 'molv'
        });

        table.render({
            elem: '#currentTableId',
            url: '/admin/user',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                    {type: "checkbox", width: 50, fixed: "left"},
                    {field: 'id', width: 80, title: 'ID', sort: true},
                    {field: 'username', width: 150, title: '用户名', sort: true},
                    {field: 'nickname', width: 150, title: '昵称'},
                    {
                        field: 'sex', width: 80, title: '性别', sort: true, align: 'center', templet: function (data) {
                            let sex = data.sex ? '男' : '女';
                            let color = (data.sex ? 'blue' : 'red');
                            let bgColor = 'layui-bg-' + color;
                            return '<span class="layui-badge ' + bgColor + '">' + sex + '</span>';
                        }
                    },
                    {
                        field: 'role', title: '角色', width: 100, sort: true, align: 'center', templet: function (data) {
                            if (data.role !== undefined) {
                                let roleName = data.role.roleName;
                                let color = 'layui-bg-' + data.role.color;
                                return '<span class="layui-badge ' + color + '">' + roleName + '</span>';
                            } else {
                                return '-';
                            }
                        }
                    },
                    {
                        field: 'status',
                        width: 150,
                        title: '状态',
                        sort: true,
                        align: 'center',
                        hide: (!hasPermission('sys:user:edit')),
                        templet: function (data) {
                            let isChecked = data.status == 1 ? "checked" : "";
                            return '<input type="checkbox" lay-skin="switch" lay-text="启用|禁用" lay-filter="status-filter"' + isChecked + '> ';
                        }
                    },
                    {field: 'email', width: 150, title: '邮箱'},
                    {field: 'phone', width: 150, title: '手机号码'},
                    {field: 'createTime', title: '创建时间', minWidth: 200, sort: true},
                    {field: 'updateTime', title: '更新时间', minWidth: 200, sort: true},
                    {
                        title: '操作',
                        minWidth: 150,
                        toolbar: '#currentTableBar',
                        fixed: "right",
                        align: "center",
                        hide: (!hasPermission('sys:user:edit') && !hasPermission('sys:user:delete'))
                    }
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            done: function () {
                checkPermission();
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            let username = data.field.username;
            let email = data.field.email;
            let dateRange = data.field.dateRange;
            let dates = dateRange.split(' ~ ')
            let startDate = dates[0];
            let endDate = dates[1];

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    username: username,
                    email: email,
                    startDate: startDate,
                    endDate: endDate
                }
            }, 'data');

            return false;
        });

        //监听状态开关
        form.on('switch(status-filter)', function (obj) {
            let status = obj.elem.checked ? 1 : 0;
            let userId = obj.othis.parents('tr').find("td").eq(1).text();
            let username = obj.othis.parents('tr').find("td").eq(2).text()
            let op = (status === 1 ? '启用' : '停用');
            let msg = '是否' + op + '用户:' + username + '?';
            let x = obj.elem.checked;
            Swal.fire({
                title: msg,
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: '确定！',
                cancelButtonText: '取消！',
                showLoaderOnConfirm: true,
                preConfirm: () => {
                    return new Promise((resolve, reject) => {
                        axios({
                            method: 'put',
                            url: '/admin/user/status',
                            data: {
                                id: userId,
                                status: status,
                            },
                            headers: {
                                'X-Requested-With': 'XMLHttpRequest'
                            }
                        })
                            .then(response => {
                                resolve(response.data);
                            }, error => {
                                reject(error);
                            })
                    }).then(res => {
                        Swal.fire({
                            icon: 'success',
                            title: '修改成功！',
                            showConfirmButton: false,
                            timer: 1500
                        });
                    }).catch(error => {
                        Swal.showValidationMessage(`修改失败: ${error}`);
                    })
                }
            }).then((result) => {
                if (result.dismiss === Swal.DismissReason.cancel) {
                    obj.elem.checked = !x;
                }
                form.render();
            })
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '80%'],
                    content: '/admin/page/user/user-add',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                var idList = [];
                $.each(data, function () {
                    idList.push($(this)[0].id);
                });
                if (idList.length <= 0) {
                    return false;
                }
                Swal.fire({
                    title: '确定删除选中的' + idList.length + '个用户吗？',
                    text: '你将无法恢复它！',
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: '确定删除！',
                    cancelButtonText: '取消删除！',
                    showLoaderOnConfirm: true,
                    preConfirm: () => {
                        return new Promise((resolve, reject) => {
                            axios({
                                method: 'delete',
                                url: '/admin/user',
                                data: idList,
                                headers: {
                                    'X-Requested-With': 'XMLHttpRequest'
                                }
                            })
                                .then(response => {
                                    table.reload('currentTableId');
                                    resolve(response.data);
                                }, error => {
                                    reject(error);
                                })
                        }).then(res => {
                            Swal.fire({
                                icon: 'success',
                                title: '删除成功！',
                                showConfirmButton: false,
                                timer: 1500
                            });
                        }).catch(error => {
                            Swal.showValidationMessage(`删除失败: ${error.response.data.message}`);
                        })
                    }
                });
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '80%'],
                    content: '/admin/user/' + data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                Swal.fire({
                    title: '确定删除选中的这个用户吗？',
                    text: '你将无法恢复它！',
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: '确定删除！',
                    cancelButtonText: '取消删除！',
                    showLoaderOnConfirm: true,
                    preConfirm: () => {
                        return new Promise((resolve, reject) => {
                            axios({
                                method: 'delete',
                                url: '/admin/user/' + data.id,
                                headers: {
                                    'X-Requested-With': 'XMLHttpRequest'
                                }
                            })
                                .then(response => {
                                    obj.del();
                                    resolve(response.data);
                                }, error => {
                                    reject(error);
                                })
                        }).then(res => {
                            Swal.fire({
                                icon: 'success',
                                title: '删除成功！',
                                showConfirmButton: false,
                                timer: 1500
                            });
                        }).catch(error => {
                            Swal.showValidationMessage(`删除失败: ${error.response.data.message}`);
                        })
                    }
                });
            }
        });

    });
</script>
</body>
</html>